package com.zhw.goshopping.manager.service;

import com.zhw.goshopping.common.pojo.EasyUITreeNode;
import com.zhw.goshopping.dao.TbItemCatMapper;
import com.zhw.goshopping.pojo.TbItemCat;
import com.zhw.goshopping.pojo.TbItemCatExample;
import com.zhw.goshopping.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by zhaihongwei on 2018/3/19
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    /**
     * 根据父id查询商品分类
     * @param parentId
     * @return
     */
    @Override
    public List<EasyUITreeNode> getItemCatList(long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        tbItemCatMapper.selectByExample(example);
        // 设置查询条件
        TbItemCatExample.Criteria criteria = example.createCriteria();
        // 设置parentId
        criteria.andParentIdEqualTo(parentId);
        // 执行查询
        List<TbItemCat> cats = tbItemCatMapper.selectByExample(example);
        // 转换成EasyUITreeNode
        List<EasyUITreeNode> resultList = new ArrayList<>();
        for (TbItemCat tbItemCat : cats) {
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(tbItemCat.getId());
            node.setText(tbItemCat.getName());
            // 如果节点有子节点“closed”，如果没有子节点“open”
            node.setState(tbItemCat.getIsParent()?"closed":"open");
            //添加到节点列表
            resultList.add(node);
        }
        return resultList;
    }
}
